package com.iussoft.demo;

import com.iussoft.demo.ftp.FtpService;
import com.iussoft.demo.jms.MQRecevicer;
import com.iussoft.demo.tcp.TcpServiceImpl;
import com.iussoft.demo.util.ConfigUtil;
import com.iussoft.demo.ws.BaseServiceImpl;

import javax.xml.ws.Endpoint;
import java.io.File;
import java.net.URL;
import java.util.Properties;

/**
 * @description: 启动类
 * @author: Hou Dayu
 * @date: Created in 2020/10/27
 */
public class PushServerApplication {

    private static final String WS_CONFIG_PATH = "application.properties";

    private static void initConf(){
        String path = "";
        URL classLoaderUrl = Thread.currentThread().getContextClassLoader().getResource("");
        if (null == classLoaderUrl) {
            path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
        } else {
            path = classLoaderUrl.getPath();
        }
        if (!path.endsWith(File.separator) && !path.endsWith("/")) {
            path = path + "/";
        }
        Properties pro = ConfigUtil.initWS(new File(path+WS_CONFIG_PATH));
        if ((pro == null) || (pro.keySet().size() == 0)) {
            System.out.println("配置文件未找到application.properties");
            System.exit(0);
        }
        ConfigUtil.WSPORT = pro.getProperty("ws_port");
    }

    public static void main(String[] args) {
        initConf();
        if("T".equalsIgnoreCase(ConfigUtil.getConfigByKey("webservice.open"))) {
            publishWS();
        }
        if("T".equalsIgnoreCase(ConfigUtil.getConfigByKey("tcp.open"))) {
            publishTcp();
        }
        if("T".equalsIgnoreCase(ConfigUtil.getConfigByKey("ftp.open"))) {
            new FtpService().startFtpServer();
        }
        if("T".equalsIgnoreCase(ConfigUtil.getConfigByKey("jms.open"))) {
            new MQRecevicer().startJmsServer();
        }
    }

    public static void publishWS(){
        System.out.println("Endpoint方式发布webService");
        try {
            String baseUrl = ConfigUtil.getWsIpAndPort();
            System.out.println("baseUrl:" + baseUrl);
            BaseServiceImpl testConn = new BaseServiceImpl();
            String url1 = baseUrl + "/baseService";
            Endpoint ep1 = Endpoint.publish(url1, testConn);
            System.out.println("以下是访问地址");
            System.out.println(url1 + "?wsdl");
        } catch (Exception e) {
            System.out.println("发布出现异常！");
            e.printStackTrace();
        }
    }

    public static void publishTcp(){
        TcpServiceImpl tcpService = new TcpServiceImpl();
        tcpService.pushTcpService();
    }
}
